package educationTool.control;

import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;

import educationTool.tools.ParserCSV;

import educationTool.model.Pair;
import educationTool.model.User;

/**
 * This class works with schedule of specific user.
 * It gives possibilities to load new schedule and to do selections of pairs on some period.
 */
public class ScheduleHandler {
	
	private User user;
	
	/**
	 * Constructs a handler
	 * @param user whose schedule will be handled
	 * @throws NullPointerException if <b>user</b> is null
	 */
	public ScheduleHandler(User user){
		if(user!=null) {
			this.user = user;
                }
                else {
			throw new NullPointerException();
                }
	}
	
	/**
	 * Loads user's schedule from a *.csv file
	 * @param path - absolute path to the file
	 */
	public void loadSchedule(String path){
        //try {
            user.setLessonList((LinkedList<Pair>) ParserCSV.getInstance().parseCSV(path));
        //} catch (IOException e) { //never thrown
            //e.printStackTrace();
        //} catch (ParseException e) { //never thrown
            //e.printStackTrace();
        //}
    }
	
	public LinkedList<Pair> getTodayLessons(){
		LinkedList<Pair> result = new LinkedList<Pair>();
		Calendar today = Calendar.getInstance();
		Calendar lessonDate = Calendar.getInstance();
		for(Pair lesson : user.getLessonList()){
			lessonDate.setTime(lesson.getDateBegin());
			if(today.get(Calendar.YEAR)==lessonDate.get(Calendar.YEAR) 	&& 
				today.get(Calendar.MONTH)==lessonDate.get(Calendar.MONTH) && 
				today.get(Calendar.DAY_OF_MONTH)==lessonDate.get(Calendar.DAY_OF_MONTH))
			{
				result.add(lesson);
			}
		}
		return result;
	}
	
	public LinkedList<Pair> getTomorrowLessons(){
		LinkedList<Pair> result = new LinkedList<Pair>();
		Calendar tomorrow = Calendar.getInstance();
		tomorrow.add(Calendar.DAY_OF_MONTH, 1);
		Calendar lessonDate = Calendar.getInstance();
		
		for(Pair lesson : user.getLessonList()){
			lessonDate.setTime(lesson.getDateBegin());
			if(tomorrow.get(Calendar.YEAR)==lessonDate.get(Calendar.YEAR) 	&& 
				tomorrow.get(Calendar.MONTH)==lessonDate.get(Calendar.MONTH) && 
				tomorrow.get(Calendar.DAY_OF_MONTH)==lessonDate.get(Calendar.DAY_OF_MONTH))
			{
				result.add(lesson);
			}
		}
		return result;
	}
	
	public LinkedList<Pair> getCurrentWeekLessons() {
		LinkedList<Pair> result = new LinkedList<Pair>();
		Calendar today = Calendar.getInstance();
		Calendar lessonDate = Calendar.getInstance();
		for(Pair lesson : user.getLessonList()){
			lessonDate.setTime(lesson.getDateBegin());
			if(today.get(Calendar.YEAR)==lessonDate.get(Calendar.YEAR) 	&& 
				today.get(Calendar.MONTH)==lessonDate.get(Calendar.MONTH) && 
				today.get(Calendar.WEEK_OF_MONTH)==lessonDate.get(Calendar.WEEK_OF_MONTH))
			{
				result.add(lesson);
			}
		}
		return result;
	}

	public LinkedList<Pair> getNextWeekLessons() {
		LinkedList<Pair> result = new LinkedList<Pair>();
		Calendar next_week = Calendar.getInstance();
		next_week.add(Calendar.WEEK_OF_MONTH, 1);
		Calendar lessonDate = Calendar.getInstance();
		
		for(Pair lesson : user.getLessonList()){
			lessonDate.setTime(lesson.getDateBegin());
			if(next_week.get(Calendar.YEAR)==lessonDate.get(Calendar.YEAR) 	&& 
				next_week.get(Calendar.MONTH)==lessonDate.get(Calendar.MONTH) && 
				next_week.get(Calendar.WEEK_OF_MONTH)==lessonDate.get(Calendar.WEEK_OF_MONTH))
			{
				result.add(lesson);
			}
		}
		return result;
	}
	
	public LinkedList<Pair> getLessonsForPeriod(Date begin, Date end){
		LinkedList<Pair> result = new LinkedList<Pair>();
		
		Calendar beginDate = Calendar.getInstance();
		beginDate.setTime(begin);
		Calendar endDate = Calendar.getInstance();
		endDate.setTime(end);
		endDate.set(Calendar.HOUR_OF_DAY, 23);
		endDate.set(Calendar.MINUTE, 59);
		endDate.set(Calendar.SECOND, 59);
		Calendar lessonDateBegin = Calendar.getInstance();
		Calendar lessonDateEnd = Calendar.getInstance();
		
		for(Pair lesson : user.getLessonList()){
			lessonDateBegin.setTime(lesson.getDateBegin());
			lessonDateEnd.setTime(lesson.getDateEnd());
			if(lessonDateBegin.after(beginDate) && lessonDateEnd.before(endDate))
			{
				result.add(lesson);
			}
		}
		return result;
	}
}
